package com.xujiakai.car.maintain.application.service.impl;


import com.github.pagehelper.PageHelper;
import com.xujiakai.car.maintain.application.service.ISystemManageService;
import com.xujiakai.car.maintain.domain.entity.Permission;
import com.xujiakai.car.maintain.domain.entity.Userinfo;
import com.xujiakai.car.maintain.domain.dto.PermissionDTO;
import com.xujiakai.car.maintain.domain.dto.SectorDTO;
import com.xujiakai.car.maintain.infrastructure.mapper.DaoFactory;
import com.xujiakai.car.maintain.infrastructure.utils.BeanUtil;
import com.xujiakai.car.maintain.infrastructure.utils.PagedResult;
import com.xujiakai.car.maintain.userinterface.views.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统基础数据服务的service
 *
 * @author cp
 */
@Service
public class SystemManageService implements ISystemManageService {

    public static final Logger logger = LoggerFactory.getLogger(SystemManageService.class);

    @Resource
    private DaoFactory daoFactory;

    @Resource
    Userinfo userinfoDTO;

    @Resource
    Permission permissionEntity;

    /**
     * 查询所有的用户
     */
    @Override
    public PagedResult<UserView> queryAllUser(Integer pageNo, Integer pageSize, String status, String key) {
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.topagedResult(daoFactory.getUserinfoMapper().selectAllUsers(status, key));
    }

    /**
     * 修改用户操作
     */
    @Transactional
    @Override
    public boolean modifyUser(UserView userView, int[] privileges, String type) {
        return userinfoDTO.modifyUser(userView, privileges, type);
    }


    /**
     * 查询所有合法的权限
     */
    @Override
    public List<PermissionDTO> queryAllPermission() {
        return daoFactory.getPermissionMapper().selectAllPermissions();
    }

    /**
     * 查询所有合法的权限
     */
    @Override
    public List<PermissionDTO> queryAllComment() {
        return daoFactory.getPermissionMapper().selectAllPermissions();
    }


    /**
     * 查询所有的部门
     */
    @Override
    public List<SectorDTO> queryAllSectors() {
        return daoFactory.getSectorMapper().selectAllSector();
    }


    /**
     * 查询所有的用户的权限
     */
    @Override
    public List<PermissionDTO> queryAllUserPer(String accountnumber) {
        return daoFactory.getPermissionMapper().getAllUserPermission(accountnumber);
    }

    /**
     * 修改密码
     */
    @Override
    public boolean modifyPass(String password, String accountnumber) {
        return permissionEntity.modifyPass(password, accountnumber);
    }

}
